﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_MaterialOrderDetail : Form
    {
        public int detail_id;

        private INISettings mySetting;
        private SqlConnection myConnection;

        public frm_Edit_MaterialOrderDetail()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.detail_id = 0;            
        }
        /////////////////////////////////////
        /// Load Data
        ////////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;
            if (detail_id == 0)
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT [444_MaterialOrderDetail].OrderNo, [444_Material].PartNo,"
                        + " [444_MaterialOrderDetail].Quantity, [444_MaterialOrderDetail].DeadLine,"
                        + " [444_Material].RoHS_RRI, [444_Material].Price, [444_Material].QuantityPerUnit"
                        + " FROM [444_MaterialOrderDetail] LEFT OUTER JOIN"
                        + " [444_Material] ON [444_Material].ID=[444_MaterialOrderDetail].MaterialID"
                        + " WHERE [444_MaterialOrderDetail].ID=" + detail_id;
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.orderNo.Text = dataReader["OrderNo"].ToString();
                        this.materialID.Text = dataReader["PartNo"].ToString();                        
                        this.roHS.Text = dataReader["RoHS_RRI"].ToString();
                        this.priceValue.Text = dataReader["Price"].ToString();
                        this.quantityUnit.Text = dataReader["QuantityPerUnit"].ToString();
                        this.deadLine.EditValue = DateTime.Parse(dataReader["DeadLine"].ToString());
                        this.quantityValue.Text = dataReader["Quantity"].ToString();
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }                    
                    MessageBox.Show("Cannot get Material information");
                }
            }

            return ret;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {            
            DateTime dead_line = new DateTime();
            int quantity = 0;
            string qty = null;
            
            if (this.deadLine.EditValue != null)
            {
                dead_line = DateTime.Parse(this.deadLine.EditValue.ToString());
            }

            if (!string.IsNullOrEmpty(this.quantityValue.Text))
            {
                if (Int32.TryParse(this.quantityValue.Text, out quantity))
                {
                    qty = quantity.ToString();
                }
            }

            try
            {
                myConnection.Open();
                string sql = "UPDATE [444_MaterialOrderDetail] SET Quantity=" + qty + ", DeadLine=@deadline"
                    + " WHERE ID=" + detail_id;
                SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                cmd_sql.Parameters.Add("@deadline", SqlDbType.DateTime).Value = dead_line.Date;
                cmd_sql.ExecuteNonQuery();
                cmd_sql.Dispose();
                myConnection.Close();
                MessageBox.Show("Material Orther Detail updated success.");
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot update Material Order Detail. Please try again.");
            }            
        }
    }
}
